
from homeassistant.const import (ATTR_ENTITY_ID,ATTR_FRIENDLY_NAME,ATTR_DOMAIN)

from .const import (
    NULL_VALUE ,
)

HVAC_MODE_AUTO = 'auto'
SERVICE_SET_FAN_MODE = 'set_fan_mode'
SERVICE_SET_HVAC_MODE = 'set_hvac_mode'
SERVICE_SET_TEMPERATURE = 'set_temperature'
ATTR_HVAC_MODE = 'hvac_mode'


def get_props(cur_dev,dev):
        """返回空调属性
        """
        if dev['name'] == 'power':
            dev['value']  = 'off' if cur_dev['state'] == 'off' else 'on'
        elif dev['name'] == 'mode':
            dev['value'] = cur_dev['state']
        elif dev['name'] == "current_temperature":
            dev['value'] = cur_dev['attributes']['current_temperature'] if 'current_temperature' in cur_dev['attributes'] else NULL_VALUE
        elif dev['name'] == "temperature":
            dev['value'] = cur_dev['attributes']['temperature'] if 'temperature' in cur_dev['attributes'] else NULL_VALUE
        elif dev['name'] == 'fan_mode':
            dev['value'] = cur_dev['attributes']['fan_mode']   if 'fan_mode' in cur_dev['attributes'] else NULL_VALUE
        else:
            dev['value'] = NULL_VALUE

        if dev['value'] == None:
            dev['value'] = NULL_VALUE
        return dev


def set_props_to_call_service_data(cur_dev,dev):
    """设置开关"""
    domain = cur_dev[ATTR_ENTITY_ID].split('.')[0]

    data = {ATTR_ENTITY_ID:cur_dev[ATTR_ENTITY_ID]}
    service = ''
    if dev['name'] == 'power':
        service = 'turn_off' if dev['value'] == 'off' else SERVICE_SET_HVAC_MODE
        if dev['value'] != 'off':
            data[ATTR_HVAC_MODE]=HVAC_MODE_AUTO

    elif dev['name'] == 'mode':
        service = SERVICE_SET_HVAC_MODE
        data[ATTR_HVAC_MODE]=dev['value']

    elif dev['name'] == "target-temperature":
        data['temperature'] = dev['value']
        service = SERVICE_SET_TEMPERATURE

    elif dev['name'] == "fan-level":
        data['fan_mode'] = dev['value']
        service = SERVICE_SET_FAN_MODE

    return [domain,service,data]